from core.base import BaseHandler
from messages import ProtocalTypes
from persist.daos import companyDao,projectDao,employeeDao,deliveryDao,dishDao
from transistors import CompanyTransistor,ProjectTransistor,EmployeeTransistor,DeliveryTransistor,DishTransistor
from persist.cachedaos import companyCacheDao,projectCacheDao

class GetCompanyHandler(BaseHandler):
    
    def __init__(self):
        self.companyCacheDao = companyCacheDao
    
    def process(self,protocal,transport):
        if not protocal:
            pass
        def send(company):
            transport.write({'type':ProtocalTypes.PushCompany,'data':CompanyTransistor.toJson(company)})
        self.companyCacheDao.getCompany(send)
        
class GetProjectsHandler(BaseHandler):
    
    def __init__(self):
        self.projectCacheDao = projectCacheDao

    def process(self,protocal,transport):
        if not protocal:
            pass
        def send(projects):
            projectJsons = [ProjectTransistor.toJson(project) for project in projects]
            transport.write({'type':ProtocalTypes.PushProjects,'data':projectJsons});
        self.projectCacheDao.getAllProject(protocal.companyId, send)

class GetEmployeesHandler(BaseHandler):
    
    def __init__(self):
        self.employeeDao = employeeDao

    def process(self,protocal,transport):
        if not protocal :
            pass
        employees = self.employeeDao.getProjectEmployee(protocal.projectId)
        employeeJsons = [EmployeeTransistor.toJson(employee) for employee in employees]
        transport.write({'type':ProtocalTypes.PushProjectEmployees,'data':employeeJsons})

class GetDeliveriesHandler(BaseHandler):
    
    def __init__(self):
        self.deliveryDao = deliveryDao
    
    def process(self,protocal,transport):
        if not protocal:
            return
        deliveries = self.deliveryDao.getAllDeliveries()
        deliveriesJsons = [DeliveryTransistor.toJson(delivery) for delivery in deliveries]
        transport.write({'type':ProtocalTypes.PushDeliveries,'data':deliveriesJsons})

class GetDishesHandler(BaseHandler):
    
    def __init__(self):
        self.dishDao = dishDao
    
    def process(self,protocal,transport):
        if not protocal:
            return
        dishes = self.dishDao.getDeliveryDishes(protocal.deliveryId)
        dishJsons = [DishTransistor.toJson(dish) for dish in dishes]
        transport.write({'type':ProtocalTypes.PushDeliveryDishes,'data':dishJsons})

class AddProjectHandler(BaseHandler):
    
    def __init__(self):
        self.companyDao = companyDao
        self.projectDao = projectDao
    
    def process(self,protocal,transport):
        if not protocal:
            return
        company = companyDao.getCompany()
        project = projectDao.addProject(protocal.projectName, protocal.projectIntro, company)
        transport.write({'type':ProtocalTypes.PushProject,'data':ProjectTransistor.toJson(project)})